#########################################################################################
####################################  FIGURE 1   ########################################
#############  Women’s Influence and Authority by Group Gender Composition  #############
#########################################################################################

rm(list = ls())

need <- c("here", "tidyverse", "readxl", "glue", "gridExtra", "dplyr", "stats","ggpubr")
have <- need %in% rownames(installed.packages())
if (any(!have)) install.packages(need[!have])
invisible(lapply(need, library, character.only = T))

#setwd("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_1")
set.seed(12345)
#########################################################################################


###Figure 1a###

# Import Study 1 analysis produced in STATA do file
study1_minfl <- read.csv("intermediate_data/study1_groupgenderminfl_labavg_ratio_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_linfl <- read.csv("intermediate_data/study1_groupgenderlinfl_labavg_ratio_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_minflp <- read_excel("intermediate_data/study1_groupgenderminfl_labavg_ratio_1p.xlsx")
study1_minflp$p <- as.character(study1_minflp$p)
study1_minflp$p[study1_minflp$p == '0'] <- '<0.001'
study1_linflp <- read_excel("intermediate_data/study1_groupgenderlinfl_labavg_ratio_1p.xlsx")
study1_linflp$p <- as.character(study1_linflp$p)
study1_linflp$p[study1_linflp$p == '0'] <- '<0.001'

# Import Study 2 analysis produced in STATA do file
study2_minfl <- read.csv("intermediate_data/study2_groupgenderminfl_ratio_r1_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_linfl <- read.csv("intermediate_data/study2_groupgenderlinfl_ratio_r1_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_minflp <- read_excel("intermediate_data/study2_groupgenderminfl_ratio_r1_1p.xlsx")
study2_minflp$p <- as.character(study2_minflp$p)
study2_minflp$p[study2_minflp$p == '0'] <- '<0.001'
study2_linflp <- read_excel("intermediate_data/study2_groupgenderlinfl_ratio_r1_1p.xlsx")
study2_linflp$p <- as.character(study2_linflp$p)
study2_linflp$p[study2_linflp$p == '0'] <- '<0.001'

# Import combined analysis produced in STATA do file
combined_minfl <- read.csv("intermediate_data/combined_groupgenderminfl_ratio_ravg_forgraphing.csv") %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_linfl <- read.csv("intermediate_data/combined_groupgenderlinfl_ratio_ravg_forgraphing.csv") %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_minflp <- read_excel("intermediate_data/combined_groupgenderminfl_ratio_ravg_1p.xlsx")
combined_minflp$p <- as.character(combined_minflp$p)
combined_minflp$p[combined_minflp$p == '0'] <- '<0.001'
combined_linflp <- read_excel("intermediate_data/combined_groupgenderlinfl_ratio_ravg_1p.xlsx")
combined_linflp$p <- as.character(combined_linflp$p)
combined_linflp$p[combined_linflp$p == '0'] <- '<0.001'

# Initiate figure file
png(filename = "figure_1a.png", width = 1300, height = 800)
# windowsFonts(A = windowsFont("CMU Serif"))

# Study 1 Most Influential Figure
study1_minflvotes <- ggplot(study1_minfl, aes(x=v1, y=v2), fill="dodgerblue3") + 
  geom_bar(data=study1_minfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .6) + 
  geom_errorbar(data=study1_minfl, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) + 
  xlab("Study 1") + 
  ylab("Votes per woman") + 
  theme(text=element_text(size=20), 
        plot.title = element_text(hjust=.5), 
        axis.title.y=element_text(size=20), 
        axis.title.x=element_text(size=16), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour="black")) + 
  geom_hline(yintercept = 1, linetype="dashed") + 
  coord_cartesian(ylim = c(0, 1.3))
study1_minflvotes <- study1_minflvotes + stat_pvalue_manual(study1_minflp, label = "p={p}", size=5)


# Study 2 Most Influential Figure
study2_minflvotes <- ggplot(study2_minfl, aes(x=v1, y=v2), fill="dodgerblue3") + 
  geom_bar(data=study2_minfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .6) + 
  geom_errorbar(data=study2_minfl, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) + 
  xlab("Study 2") + 
  theme(text=element_text(size=20), 
        plot.title = element_text(hjust=.5), 
        axis.title=element_text(size=16), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.title.y = element_blank(), 
        axis.text.y = element_blank(), 
        axis.ticks.y = element_blank(), 
        axis.line = element_line(colour="black")) + 
  geom_hline(yintercept = 1, linetype="dashed") + 
  coord_cartesian(ylim = c(0, 1.3))
study2_minflvotes <- study2_minflvotes + stat_pvalue_manual(study2_minflp, label = "p={p}",  size=5)

# Combined Most Influential Figure
combined_minflvotes <- ggplot(combined_minfl, aes(x=v1, y=v2), fill="dodgerblue3") + 
  geom_bar(data=combined_minfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .6) + 
  geom_errorbar(data=combined_minfl, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) + 
  xlab("Combined") + 
  theme(text=element_text(size=20), 
        plot.title = element_text(hjust=.5), 
        axis.title=element_text(size=16), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.title.y = element_blank(), 
        axis.text.y = element_blank(), 
        axis.ticks.y = element_blank(), 
        axis.line = element_line(colour="black")) + 
  geom_hline(yintercept = 1, linetype="dashed") + 
  coord_cartesian(ylim = c(0, 1.3))
combined_minflvotes <- combined_minflvotes + stat_pvalue_manual(combined_minflp, label = "p{p}",  size=5)

# Combine figures and write to file
inflvotes <- grid.arrange(study1_minflvotes, study2_minflvotes, combined_minflvotes, nrow = 1)
inflvotes
dev.off()

###Figure 1b###

# Import Study 1 analysis produced in STATA do file
study1_spokesc <- read.csv("intermediate_data/study1_groupgenderspokes_labavg_ratio_forgraphing.csv") %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_spokescp <- read_excel("intermediate_data/study1_groupgenderspokes_labavg_ratio_2p.xlsx")
study1_spokescp$p <- as.character(study1_spokescp$p)
study1_spokescp$p[study1_spokescp$p == '0'] <- '<0.001'

# Import Study 2 analysis produced in STATA do file
study2_spokesc <- read.csv("intermediate_data/study2_genderspokes_ratio_r1_forgraphing.csv") %>%
  arrange(v1) %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_spokescp <- read_excel("intermediate_data/study2_genderspokes_ratio_r1_2p.xlsx")
study2_spokescp$p <- as.character(study2_spokescp$p)
study2_spokescp$p[study2_spokescp$p == '0'] <- '<0.001'

# Import Combined analysis produced in STATA do file
combined_spokesc <- read.csv("intermediate_data/combined_genderspokes_ratio_ravg_forgraphing.csv") %>%
  arrange(v1) %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_spokescp <- read_excel("intermediate_data/combined_genderspokes_ratio_ravg_2p.xlsx")
combined_spokescp$p <- as.character(combined_spokescp$p)
combined_spokescp$p[combined_spokescp$p == '0'] <- '<0.001'

# Initiate figure file
png(filename = "figure_1b.png", width = 1300, height = 800)

# Study 1 Figure
study1_spokesfigc <- ggplot(study1_spokesc, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(position=position_dodge(), stat="identity", colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.1, width = .6) +
  geom_errorbar(aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 1") +
  ylab("Votes per woman") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  coord_cartesian(ylim = c(0, 1.5))
study1_spokesfigc <- study1_spokesfigc + stat_pvalue_manual(study1_spokescp, label = "p={p}", size=5)

# Study 2 Figure
study2_spokesfigc <- ggplot(study2_spokesc, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(position=position_dodge(), stat="identity", colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.07, width = .6) +
  geom_errorbar(aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 2") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  coord_cartesian(ylim = c(0, 1.5))
study2_spokesfigc <- study2_spokesfigc + stat_pvalue_manual(study2_spokescp, label = "p={p}0", size=5)

# Combined Figure
combined_spokesfigc <- ggplot(combined_spokesc, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(position=position_dodge(), stat="identity", colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.07, width = .6) +
  geom_errorbar(aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Combined") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  coord_cartesian(ylim = c(0, 1.5))
combined_spokesfigc <- combined_spokesfigc + stat_pvalue_manual(combined_spokescp, label = "p={p}", size=5)

# Combine figures and write to file
spokesfigc <- grid.arrange(study1_spokesfigc, study2_spokesfigc, combined_spokesfigc, nrow = 1)
spokesfigc
dev.off()


###Figure 1c###

# Import Study 1 analysis produced in STATA do file
study1_minfl <- read.csv("intermediate_data/study1_groupgenderminfl_f2avg_ratio_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_linfl <- read.csv("intermediate_data/study1_groupgenderlinfl_f2avg_ratio_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_minflp <- read_excel("intermediate_data/study1_groupgenderminfl_f2avg_ratio_4p.xlsx")
study1_minflp$p <- as.character(study1_minflp$p)
study1_minflp$p[study1_minflp$p == '0'] <- '<0.001'
study1_linflp <- read_excel("intermediate_data/study1_groupgenderlinfl_f2avg_ratio_4p.xlsx")
study1_linflp$p <- as.character(study1_linflp$p)
study1_linflp$p[study1_linflp$p == '0'] <- '<0.001'

# Import Study 2 analysis produced in STATA do file
study2_minfl <- read.csv("intermediate_data/study2_groupfemvotesminfl_avgpre_ratio_forgraphing.csv")  %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_linfl <- read.csv("intermediate_data/study2_groupfemvoteslinfl_avgpre_ratio_forgraphing.csv")  %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_minflp <- read_excel("intermediate_data/study2_groupfemvotesminfl_avgpre_ratio_4p.xlsx")
study2_minflp$p <- as.character(study2_minflp$p)
study2_minflp$p[study2_minflp$p == '0'] <- '<0.001'
study2_linflp <- read_excel("intermediate_data/study2_groupfemvoteslinfl_avgpre_ratio_4p.xlsx")
study2_linflp$p <- as.character(study2_linflp$p)
study2_linflp$p[study2_linflp$p == '0'] <- '<0.001'

# Import Combined analysis produced in STATA do file
combined_minfl <- read.csv("intermediate_data/combined_groupfemvotesminfl_avgpre_ratio_forgraphing.csv")  %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_linfl <- read.csv("intermediate_data/combined_groupfemvoteslinfl_avgpre_ratio_forgraphing.csv")  %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_minflp <- read_excel("intermediate_data/combined_groupfemvotesminfl_avgpre_ratio_4p.xlsx")
combined_minflp$p <- as.character(combined_minflp$p)
combined_minflp$p[combined_minflp$p == '0'] <- '<0.001'
combined_linflp <- read_excel("intermediate_data/combined_groupfemvoteslinfl_avgpre_ratio_4p.xlsx")
combined_linflp$p <- as.character(combined_linflp$p)
combined_linflp$p[combined_linflp$p == '0'] <- '<0.001'

# Initiate figure file
png(filename = "figure_1c.png", width = 1300, height = 800)
study1_minflp$y.position <- 1.2
study2_minflp$y.position <- 1.2
combined_minflp$y.position <- 1.2
# Study 1 Most Influential Figure
study1_minflvotes <- ggplot(study1_minfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=study1_minfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .6) +
  geom_errorbar(data=study1_minfl, aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 1") +
  ylab("Votes per woman") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title.y=element_text(size=20),
        axis.title.x=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0,1.25))
study1_minflvotes <- study1_minflvotes + stat_pvalue_manual(study1_minflp, label = "p={p}", size=5)

# Study 2 Most Influential Figure
study2_minflvotes <- ggplot(study2_minfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=study2_minfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=study2_minfl, aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 2") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title.x=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0,1.25))
study2_minflvotes <- study2_minflvotes + stat_pvalue_manual(study2_minflp, label = "p={p}", size=5)

# Combined Most Influential Figure
combined_minflvotes <- ggplot(combined_minfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=combined_minfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=combined_minfl, aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Combined") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title.x=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0,1.25))
combined_minflvotes <- combined_minflvotes + stat_pvalue_manual(combined_minflp, label = "p={p}", size=5)

# Combine figures and write to file
inflvotes <- grid.arrange(study1_minflvotes, study2_minflvotes, combined_minflvotes, nrow = 1)
inflvotes
dev.off()

